using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Xml;

public delegate void OnMouseOverSpellIconDelegate(SpellIcon spellIcon, bool val);
public delegate void OnMouseClicSpellIconDelegate(SpellIcon spellIcon);
public delegate void OnMouseClicReadyDelegate();

public class Hud : MonoBehaviour {
	
	public DarkArena darkArena;
	public GameManager gameManager;
	public Networking networkManager;
	
	//----------------- POPUP--------------------------------------------
	public Popup popup;
	
	//----------------- CLASS--------------------------------------------
	public GameObject sceneSelectClass;
	public GameObject waitingPlayer;
	public Card WarCard;
	public Card WizCard;
	public Card RogCard;
	
	//----------------- SPELLS--------------------------------------------
	public GameObject sceneSelectSpells;
	public Transform counterSpellParent;
	public Transform defensiveSpellParent;
	public Transform interuptSpellParent;
	public Transform offensiveSpellParent;
	public TimeLine timeline;
	public readyButton readyButton;
	public GameObject waitingPlayerSpells;
	//Prefab
	public GameObject prefabSpellIcon;
	// list SpellIcon
	List<SpellIcon> spellIconList= new List<SpellIcon>();
	
	//----------------- FightingScene--------------------------------------------
	
	//Fighting Scene
	
	public GameObject fightingSceneParent;
	

	
	public DisplayRound displayround;
	
	
	void Awake()
	{
		QuitAllScenes();
	}
	
	
	#region Class
	
	// Initialize SelectClass
	public void InitializeSelectClassScene()
	{
		CleanCards();
		sceneSelectClass.SetActiveRecursively(true);
		waitingPlayer.SetActiveRecursively(false);
	}
	
	void CleanCards()
	{
		// collider
		WarCard.transform.collider.enabled = true;
		WizCard.transform.collider.enabled = true;
		RogCard.transform.collider.enabled = true;
		
		//material
		WarCard.transform.renderer.material.color = Color.white;
		WizCard.transform.renderer.material.color = Color.white;
		RogCard.transform.renderer.material.color = Color.white;
	}
	
	public void SelectCard(Card card)
	{
		// collider
		WarCard.transform.collider.enabled = false;
		WizCard.transform.collider.enabled = false;
		RogCard.transform.collider.enabled = false;
		
		//on colore en vert celle selectionné on grise les autres
		card.transform.renderer.material.color = Color.green;
		if ( WarCard != card )
		{
			WarCard.transform.renderer.material.color = Color.gray;
		}
		if ( WizCard != card )
		{
			WizCard.transform.renderer.material.color = Color.gray;
		}
		if ( RogCard != card )
		{
			RogCard.transform.renderer.material.color = Color.gray;
		}
		
		waitingPlayer.SetActiveRecursively(true);
		
		//send in networkmanager
		networkManager.SelectClass(card.personnageIndex);
		
	}
	
	// Quit SelectClass
	public void QuitSelectClassScene()
	{
		sceneSelectClass.SetActiveRecursively(false);
		CleanCards();
	}
	
	#endregion
	
	#region Spell
	
	// Initialize SelectSpells
	public void InitializeSelectSpells()
	{	
		sceneSelectSpells.SetActiveRecursively(true);
		waitingPlayerSpells.SetActiveRecursively(false);
		readyButton.initializeReadyButton(OnMouseClicReadyIcon);
		readyButton.gameObject.SetActiveRecursively(false);
		
		CleanSelectSpellsScene();
		
		// remove spell in networking
		networkManager.RemoveSpells();
		
		// Load Spells of player
		List<Spells> listSpellsPlayer = new List<Spells>();
		spellIconList = new List<SpellIcon>();
		listSpellsPlayer = darkArena.gameRessource.GetSpellsPersos(networkManager.personnages[Network.player]);
		
		//instanciate prefab spell
		foreach ( Spells spellPlayer in listSpellsPlayer )
		{
			Transform parent;
			switch (spellPlayer.Type)
			{
				case "Attaque":
					parent = offensiveSpellParent;
					break;
				case "Defense":
					parent = defensiveSpellParent;
					break;
				case "Contre":
					parent = counterSpellParent;
					break;
				case "Interrupt":
					parent = interuptSpellParent;
					break;
				default:
					parent = offensiveSpellParent;
					Debug.Log("ERROR SPELL TYPE "+ spellPlayer.Type+ " not found");
					break;
			}
			
			
			
			Object objectSpellIcon = Instantiate((Object)prefabSpellIcon);
			GameObject gameObjectSpellIcon = (GameObject)objectSpellIcon;
			//init position and parent
			gameObjectSpellIcon.transform.parent = parent;
			//gameObjectSpellIcon.transform.localPosition.Set( -2.204519f, -2.664373f, 0f);
			float yValue = -2.2f;
			float xValue = -3.2f;
			if ( parent.childCount>5)
			{
				yValue = -3.694552f;
				xValue = -9.2f;
			}
			gameObjectSpellIcon.transform.localPosition = new Vector3(xValue+(1.5f*(parent.childCount-1)), yValue, 0f);
			gameObjectSpellIcon.transform.localRotation = Quaternion.Euler(new Vector3(90f, 180f, 0f));
			SpellIcon spellIcon = gameObjectSpellIcon.GetComponent<SpellIcon>();
			spellIcon.InitSpellIcon(spellPlayer, OnMouseOverSpellIcon, OnMouseClicSpellIcon);
			spellIcon.GetComponentInChildren<CCText>().Text = spellIcon.Spell.NbTour.ToString();
			spellIconList.Add(spellIcon);
		}
	}
	
	//Mouse Over
    public void OnMouseOverSpellIcon(SpellIcon spellIcon, bool val)
    {
        if (val)
        {			
            popup.DisplayPopupInfo(true, spellIcon.Spell);
			spellIcon.GetComponentInChildren<CCText>().renderer.enabled = true;
        }
        else
        {
            popup.DisplayPopupInfo(false);
			spellIcon.GetComponentInChildren<CCText>().renderer.enabled = false;
        }
    }
	
	public void UpdateIconAvailable()
	{
		int countTurn = timeline.countTurns();
		foreach ( SpellIcon spellIcon in spellIconList )
		{
			if ( (spellIcon.Spell.NbTour+countTurn)>10 )
			{
				spellIcon.renderer.material.color = Color.grey;
			}
			else
			{
				spellIcon.renderer.material.color = Color.white;
			}
		}
		if (countTurn==10)
		{
			readyButton.gameObject.SetActiveRecursively(true);
		}
		else
		{
			readyButton.gameObject.SetActiveRecursively(false);
		}
	}
	
	
	// Clic on Spell
	public void OnMouseClicSpellIcon(SpellIcon spellIcon)
    {
        timeline.AddSpells(spellIcon.Spell);
    }
	
	// Clic on Ready
	public void OnMouseClicReadyIcon()
    {
		readyButton.ActiveButton(true);
		waitingPlayerSpells.SetActiveRecursively(true);
		// desactive tt les colliders de la timeline
		timeline.DesactivateColliders();
		// transform listOfSelectedSpells in two string  tours=1,2,3,5,9,1 spellId=104,101,101,102,108,101
		string tours="";
		string spellIds = "";
		int index = 0;
		foreach ( Spells spell in timeline.listOfSelectedSpells)
		{
			tours+=index.ToString()+";";
			spellIds+=spell.IdSpell.ToString()+";";
			index+=spell.NbTour;
		}
		networkManager.SelectSpells(tours,spellIds);
    }
	
	public void CleanSelectSpellsScene()
	{
		foreach ( SpellIcon spellIcon in spellIconList )
		{
			Destroy(spellIcon.gameObject);
		}
		spellIconList = new List<SpellIcon>();
		readyButton.ActiveButton(false);
		timeline.ClearTimeline();
	}
	
	// Quit SelectSpells
	public void QuitSelectSpellsScene()
	{
		sceneSelectSpells.SetActiveRecursively(false);	
		CleanSelectSpellsScene();
	}
	
	#endregion
	
	#region Fighting
	
	
	public void InitializeFightingScene()
	{
		fightingSceneParent.SetActiveRecursively(true);
		
		Personnage Perso1 = null;
		Personnage Perso2 = null;
		Dictionary<int, int> ListSpellPerso1= new Dictionary<int, int>();
		Dictionary<int, int> ListSpellPerso2= new Dictionary<int, int>();
		
		if (! gameManager.isDebugFightingScene )
		{
			
			foreach( KeyValuePair<NetworkPlayer, Dictionary<int, int>> playerSpells in networkManager.playersSpells )
			{
				if ( playerSpells.Key == Network.player )
				{
					ListSpellPerso1 = playerSpells.Value;
				}
				else
				{
					ListSpellPerso2 = playerSpells.Value;
				}
			}
			
			foreach( KeyValuePair<NetworkPlayer, Personnage> personnage in networkManager.personnages )
			{
				if ( personnage.Key == Network.player )
				{
					Perso1 = new Personnage(personnage.Value);
				}
				else
				{
					Perso2 = new Personnage(personnage.Value);
				}
			}
		}
		else
		{
			// ---------------------------------------------TEST---------------------------------------
			
			
			
			Perso1 = darkArena.CreatePersonnage(2);
			Perso2 = darkArena.CreatePersonnage(2);
			
			ListSpellPerso1.Add(0,203);
			ListSpellPerso1.Add(2,203);
			ListSpellPerso1.Add(4,203);
			ListSpellPerso1.Add(6,203);
			ListSpellPerso1.Add(8,203);
			
			ListSpellPerso2.Add(0,203);
			ListSpellPerso2.Add(2,203);
			ListSpellPerso2.Add(4,203);
			ListSpellPerso2.Add(6,203);
			ListSpellPerso2.Add(8,203);


			
			/*ListSpellPerso1.Add(0,107);
			ListSpellPerso1.Add(3,107);
			ListSpellPerso1.Add(6,107);
			ListSpellPerso1.Add(9,101);
			
			ListSpellPerso2.Add(0,306);
			ListSpellPerso2.Add(3,306);
			ListSpellPerso2.Add(6,306);
			ListSpellPerso2.Add(9,301);*/
			
			/*ListSpellPerso1.Add(0,202);
			ListSpellPerso1.Add(1,202);
			ListSpellPerso1.Add(2,202);
			ListSpellPerso1.Add(3,202);
			ListSpellPerso1.Add(4,202);
			ListSpellPerso1.Add(5,202);
			ListSpellPerso1.Add(6,202);
			ListSpellPerso1.Add(7,202);
			ListSpellPerso1.Add(8,202);
			ListSpellPerso1.Add(9,202);

			ListSpellPerso2.Add(0,101);
			ListSpellPerso2.Add(1,101);
			ListSpellPerso2.Add(2,101);
			ListSpellPerso2.Add(3,101);
			ListSpellPerso2.Add(4,101);
			ListSpellPerso2.Add(5,101);		
			ListSpellPerso2.Add(6,101);
			ListSpellPerso2.Add(7,101);
			ListSpellPerso2.Add(8,101);
			ListSpellPerso2.Add(9,101);*/
			
			/*ListSpellPerso1.Add(0,203);
			ListSpellPerso1.Add(2,203);
			ListSpellPerso1.Add(4,203);
			ListSpellPerso1.Add(6,203);
			ListSpellPerso1.Add(8,203);
			
			ListSpellPerso2.Add(0,103);
			ListSpellPerso2.Add(2,107);
			ListSpellPerso2.Add(5,102);
			ListSpellPerso2.Add(6,104);
			ListSpellPerso2.Add(8,101);
			ListSpellPerso2.Add(9,101);*/
			
			/*ListSpellPerso1.Add(0,204);
			ListSpellPerso1.Add(2,203);
			ListSpellPerso1.Add(4,203);
			ListSpellPerso1.Add(6,203);
			ListSpellPerso1.Add(8,203);
		
			ListSpellPerso2.Add(0,101);
			ListSpellPerso2.Add(1,104);
			ListSpellPerso2.Add(3,103);
			ListSpellPerso2.Add(5,104);
			ListSpellPerso2.Add(7,104);
			ListSpellPerso2.Add(9,101);*/
			//-------------------------------------------------------------------------------------------
		}
		
		displayround.BarreRessourceJ1.renderer.material.color = Perso1.CouleurHudRessource;
		displayround.BarreRessourceJ2.renderer.material.color = Perso2.CouleurHudRessource;
		
		
		displayround.GainPerteRessourceJ1.Color = Perso1.CouleurHudRessource;
		displayround.GainPerteRessourceJ2.Color = Perso2.CouleurHudRessource;
		
		Debug.Log( "Mes Sorts" );
		foreach ( KeyValuePair<int, int> ListSpellPerso in ListSpellPerso1 )
		{
			Debug.Log( ListSpellPerso.Key + " " + ListSpellPerso.Value );
		}
		Debug.Log( "Les Sorts de mon adversaire" );
		foreach ( KeyValuePair<int, int> ListSpellPerso in ListSpellPerso2 )
		{
			Debug.Log( ListSpellPerso.Key + " " + ListSpellPerso.Value );
		}
		
		displayround.Clean();
		
		displayround.MaxVieJ1.Text = "/"+Perso1.PvMax.ToString();
		displayround.MaxVieJ2.Text = "/"+Perso2.PvMax.ToString();
		displayround.MaxRessourceJ1.Text = "/"+Perso1.NbRessourceMax.ToString();
		
		StartCoroutine(darkArena.Round(ListSpellPerso1,ListSpellPerso2,Perso1,Perso2));
		
	}
	
	//AfficheRound
	//
	public IEnumerator PlayTour(int NbRound, int RoundState){//RoundState = 0 un après l'autre, RoundState = 1 les 2 en même temps
		NbRound++;
		displayround.NumeroTour.Text = "Tour N "+NbRound;
		displayround.ShowSpellIcon(NbRound);
		//Fait une attaque puis celle de l'autre joueur
		if(RoundState == 0){
			Debug.LogError("Attaque du joueur puis attaque de l'autre joueur");
			yield return StartCoroutine(displayround.Damage(NbRound,displayround.EffectSpellJ1, displayround.IconeSpellJ1,displayround.IconeSpellJ1Cross,displayround.PerteVieJ2,displayround.GainPerteBarriereJ2,displayround.FlashJ2,displayround.ChiffreVieJ2,displayround.ChiffreBarriereJ2,displayround.ChiffreRessourceJ1,displayround.GainPerteRessourceJ1,displayround.J1,displayround.J2));
			yield return StartCoroutine(displayround.Damage(NbRound,displayround.EffectSpellJ2, displayround.IconeSpellJ2,displayround.IconeSpellJ2Cross,displayround.PerteVieJ1,displayround.GainPerteBarriereJ1,displayround.FlashJ1,displayround.ChiffreVieJ1,displayround.ChiffreBarriereJ1,displayround.ChiffreRessourceJ2,displayround.GainPerteRessourceJ2,displayround.J2,displayround.J1));
			yield return StartCoroutine(displayround.Heal(NbRound,displayround.EffectSpellJ1,displayround.IconeSpellJ1,displayround.GainVieJ1,displayround.GainPerteBarriereJ1,displayround.FlashJ1,displayround.ChiffreVieJ1,displayround.ChiffreBarriereJ1,displayround.ChiffreRessourceJ1,displayround.GainPerteRessourceJ1,displayround.J1));
			yield return StartCoroutine(displayround.Heal(NbRound,displayround.EffectSpellJ2,displayround.IconeSpellJ2,displayround.GainVieJ2,displayround.GainPerteBarriereJ2,displayround.FlashJ2,displayround.ChiffreVieJ2,displayround.ChiffreBarriereJ2,displayround.ChiffreRessourceJ2,displayround.GainPerteRessourceJ2,displayround.J2));
		}
		//Fait les attaque en même temps
		if(RoundState == 1){
			Debug.LogError("Attaque des joueurs simultanée");
			StartCoroutine(displayround.Damage(NbRound,displayround.EffectSpellJ1, displayround.IconeSpellJ1,displayround.IconeSpellJ1Cross,displayround.PerteVieJ2,displayround.GainPerteBarriereJ2,displayround.FlashJ2,displayround.ChiffreVieJ2,displayround.ChiffreBarriereJ2,displayround.ChiffreRessourceJ1,displayround.GainPerteRessourceJ1,displayround.J1,displayround.J2));
			StartCoroutine(displayround.Heal(NbRound,displayround.EffectSpellJ1,displayround.IconeSpellJ1,displayround.GainVieJ1,displayround.GainPerteBarriereJ1,displayround.FlashJ1,displayround.ChiffreVieJ1,displayround.ChiffreBarriereJ1,displayround.ChiffreRessourceJ1,displayround.GainPerteRessourceJ1,displayround.J1));
			yield return StartCoroutine(displayround.Heal(NbRound,displayround.EffectSpellJ2,displayround.IconeSpellJ2,displayround.GainVieJ2,displayround.GainPerteBarriereJ2,displayround.FlashJ2,displayround.ChiffreVieJ2,displayround.ChiffreBarriereJ2,displayround.ChiffreRessourceJ2,displayround.GainPerteRessourceJ2,displayround.J2));
			yield return StartCoroutine(displayround.Damage(NbRound,displayround.EffectSpellJ2, displayround.IconeSpellJ2,displayround.IconeSpellJ2Cross,displayround.PerteVieJ1,displayround.GainPerteBarriereJ1,displayround.FlashJ1,displayround.ChiffreVieJ1,displayround.ChiffreBarriereJ1,displayround.ChiffreRessourceJ2,displayround.GainPerteRessourceJ2,displayround.J2,displayround.J1));
			yield return new WaitForSeconds(3f);
		}
		/*displayround.IconeSpellJ1.gameObject.SetActiveRecursively(false);
		displayround.IconeSpellJ1Cross.gameObject.SetActiveRecursively(false);
		displayround.IconeSpellJ2.gameObject.SetActiveRecursively(false);
		displayround.IconeSpellJ2Cross.gameObject.SetActiveRecursively(false);*/
		Debug.LogWarning("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
		yield return new WaitForSeconds(0f);
	}
	
	
	// Quit SelectClass
	public void QuitFightingScene()
	{
		fightingSceneParent.SetActiveRecursively(false);
	}
	
	
	
	
	
	
	
	#endregion
	
	// Quit AllScenes
	public void QuitAllScenes()
	{
		QuitSelectClassScene();
		QuitSelectSpellsScene();
		QuitFightingScene();
	}
	
}
